package com.glq1218.response;

import com.glq1218.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应对象
 */
@Data
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 2564255298973L;
    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 响应枚举类
     */
    private ResultEnum resultEnum;
    /**
     * 返回状态码
     */
    private Integer code;
    /**
     * 返回信息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;

    public ResponseResult(Boolean success) {
        this.success = success;
        this.code = success ? ResultEnum.SUCCESS.getCode() : ResultEnum.DEFAULT_FAIL.getCode();
        this.message = success ? ResultEnum.SUCCESS.getMessage() : ResultEnum.DEFAULT_FAIL.getMessage();
    }

    public ResponseResult(Boolean success, String message) {
        this.success = success;
        this.code = success ? ResultEnum.SUCCESS.getCode() : ResultEnum.DEFAULT_FAIL.getCode();
        this.message = message;
    }

    public ResponseResult(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public ResponseResult(Boolean success, String message, T data) {
        this.success = success;
        this.code = success ? ResultEnum.SUCCESS.getCode() : ResultEnum.DEFAULT_FAIL.getCode();
        this.message = message;
        this.data = data;
    }

    public ResponseResult(boolean success, ResultEnum resultEnum) {
        this.success = success;
        this.code = success ? ResultEnum.SUCCESS.getCode() : (resultEnum == null ? ResultEnum.DEFAULT_FAIL.getCode() : resultEnum.getCode());
        this.message = success ? ResultEnum.SUCCESS.getMessage() : (resultEnum == null ? ResultEnum.DEFAULT_FAIL.getMessage() : resultEnum.getMessage());
    }

    public ResponseResult(boolean success, ResultEnum resultEnum, T data) {
        this.success = success;
        this.code = success ? ResultEnum.SUCCESS.getCode() : (resultEnum == null ? ResultEnum.DEFAULT_FAIL.getCode() : resultEnum.getCode());
        this.message = success ? ResultEnum.SUCCESS.getMessage() : (resultEnum == null ? ResultEnum.DEFAULT_FAIL.getMessage() : resultEnum.getMessage());
        this.data = data;
    }

    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(true);
    }

    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(true, message, data);
    }

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(true, ResultEnum.SUCCESS, data);
    }

    public static <T> ResponseResult<T> error() {
        return new ResponseResult<>(false);
    }

    public static <T> ResponseResult<T> error(ResultEnum resultEnum) {
        return new ResponseResult<>(false, resultEnum);
    }

    public static <T> ResponseResult<T> error(String message) {
        return new ResponseResult<>(false, message);
    }

    public static <T> ResponseResult<T> error(Integer code, String message) {
        return new ResponseResult<>(false, code, message);
    }
}
